package org.goldenport.g3.platforms

import com.asamioffice.goldenport.text.UString.notNull
import org.goldenport.g3._

/**
 * @since   Aug. 12, 2010
 * @version Oct.  5, 2010
 * @author  ASAMI, Tomoharu
 */
class StandalonePlatform extends G3StandalonePlatform {
  def logInfo(o: AnyRef, message: Any, data: Any = None) {
    val msg = make_message("INFO", o, "", message, data)
    println(msg)
  }

  def logEnter(o: AnyRef, name: String, message: Any, data: Any = None) {
    val msg = make_message("ENTER", o, name, message, data)
    println(msg)
  }

  def logLeave(o: AnyRef, name: String, message: Any, data: Any = None) {
    val msg = make_message("LEAVE", o, name, message, data)
    println(msg)
  }

  def logDebug(o: AnyRef, message: Any, data: Any = None) {
    val msg = make_message("DEBUG", o, "", message, data)
    println(msg)
  }

  def logInfoData(o: AnyRef, message: Any)(data: => Any) {
    val msg = make_message("INFO", o, "", message, data)
    println(msg)
  }

  def logEnterData(o: AnyRef, name: String, message: Any)(data: => Any) {
    val msg = make_message("ENTER", o, name, message, data)
    println(msg)
  }

  def logLeaveData(o: AnyRef, name: String, message: Any)(data: => Any) {
    val msg = make_message("LEAVE", o, name, message, data)
    println(msg)
  }

  def logDebugData(o: AnyRef, message: Any)(data: => Any) {
    val msg = make_message("DEBUG", o, "", message, data)
    println(msg)
  }

  private def make_message(level: String, o: Object, name: String, msg: Any, data: Any): String = {
    val klass = o.getClass
    val buf = new StringBuilder
    buf.append(level)
    buf.append(' ')
    buf.append('[')
    buf.append(klass.getName)
    if (notNull(name)) {
      buf.append('.')
      buf.append(name)
    }
    buf.append(']')
    buf.append(' ')
    buf.append(msg.toString)
    if (data != None) {
      buf.append(' ')
      buf.append('{')
      make_data(data, buf)
      buf.append('}')
    }
    buf.toString
  }
}
